package com.erlin.common.utils;

import com.erlin.common.entity.PageParam;

import java.util.HashMap;
import java.util.Map;

/**
 * @author alin
 */
public class PageParamUtil {
    /**
     * 分页参数, 起始页字段名
     */
    public static final String PAGE_NO = "pageNo";
    /**
     * 分页参数, 每页大小字段名
     */
    public static final String PAGE_SIZE = "pageSize";
    /**
     * 默认值pageNo=1
     */
    public static final int DEFAULT_PAGE_NO = 1;
    /**
     * 默认pageSize=50
     */
    public static final int DEFAULT_PAGE_SIZE = 10;

    private final static ThreadLocal<Map<String, Integer>> PAGE_TL = new ThreadLocal<>();

    public static void setPageParam(int pageNo, int pageSize) {
        HashMap<String, Integer> page = new HashMap<>();
        page.put(PAGE_NO, pageNo);
        page.put(PAGE_SIZE, pageSize);
        PAGE_TL.set(page);
    }

    public static PageParam getPageParam() {
        Map<String, Integer> page = PAGE_TL.get();
        return new PageParam(page.get(PAGE_NO), page.get(PAGE_SIZE));
    }

    public static void clearPageParam() {
        Map<String, Integer> page = PAGE_TL.get();
        page.clear();
    }

    public static  void remove() {
        PAGE_TL.get().clear();
        PAGE_TL.remove();
    }
}
